﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BTv2.Controls;
using BTv2.Model;

namespace BTv2.UserControls
{
    /// <summary>
    /// Interaction logic for WheaterTile.xaml
    /// </summary>
    public partial class WheaterTile : UserControl
    {
        public WheaterTile()
        {
            InitializeComponent();
        }

        public Storyboard MarkerStoryboard;
        public Storyboard MapStoryboard;

        public async Task<bool> BuildTimelineMarkers()
        {
            var daysarray = new string[] { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
            var hour = DateTime.Now.Hour;
           Grid.SetColumn(CurrentTimeMaker,((6*24)+hour));
           int curentdayindex = 6 +(int)DateTime.Today.DayOfWeek;

           Day1.Content = daysarray[curentdayindex - 6];
           Day2.Content = daysarray[curentdayindex - 5];
           Day3.Content = daysarray[curentdayindex - 4];
           Day4.Content = daysarray[curentdayindex - 3];
           Day5.Content = daysarray[curentdayindex - 2];
           Day6.Content = daysarray[curentdayindex - 1];
           Day7.Content = daysarray[curentdayindex];
           Day8.Content = daysarray[curentdayindex + 1];
           Day9.Content = daysarray[curentdayindex + 2];
           Day10.Content = daysarray[curentdayindex + 3];
           Day11.Content = daysarray[curentdayindex + 4];
           Day12.Content = daysarray[curentdayindex + 5];
           Day13.Content = daysarray[curentdayindex + 6];

           return true;
        }

        private async void WheaterTileLoaded(object sender, RoutedEventArgs e)
        {
            //var rrrrr = DateTime.Today;
            //Grid.SetColumn(CurrentDayMarker, 12 - (7 - (int)(DateTime.Today.DayOfWeek)));

            #region story
            //Storyboard storyboard = new Storyboard();

            //// Add keyframe to turn off from element visibility
            //ObjectAnimationUsingKeyFrames kf = new ObjectAnimationUsingKeyFrames();
            //Storyboard.SetTargetProperty(kf, new PropertyPath(Grid.ColumnProperty));
            //kf.Duration = TimeSpan.FromSeconds(52);
            //kf.RepeatBehavior = RepeatBehavior.Forever;
            //kf.FillBehavior = FillBehavior.Stop;
            //for (int i = 0; i < 312; i++)
            //{
            //  kf.KeyFrames.Add(new DiscreteObjectKeyFrame(i, KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(166 * i))));
            //}

            //storyboard.Children.Add(kf);

            //// Start the storyboard
            //storyboard.Begin(CurrentTimeMaker);
            #endregion
            //for (int i = 0; i < 312; i++)
            //{
            //    Grid.SetColumn(CurrentTimeMaker,i);
            //    await Task.Delay(166);
            //}
            //DoubleAnimation moveslider = new DoubleAnimation(0, 312, new Duration(TimeSpan.FromSeconds(5)));
            //moveslider.By = 1;
            //CurrentTimeMaker.BeginAnimation(Grid.ColumnProperty, moveslider);

            WeatherContainer.DataContext = await Task.Run(()=>{ return WeatherController.GetInstance().GetWeather();});
            LoadingAnimWeather.IsActive = false;
            try
            {
                LoadingMap.IsActive = true;
                int[] dec = await Task.Run(() => { return WeatherController.GetInstance().FetchWheaterMap(); });

                WriteableBitmap decodedmap = new WriteableBitmap(500, 500, 96, 96, PixelFormats.Pbgra32, null);
                decodedmap.WritePixels(new Int32Rect(0, 0, decodedmap.PixelWidth, decodedmap.PixelHeight), dec, 4 * decodedmap.PixelWidth, 0);
                MapOverlayImage.Source = decodedmap;
                LoadingMap.IsActive = false;

                //using (FileStream stream5 = new FileStream("Test5.png", FileMode.Create))
                //{
                //    PngBitmapEncoder encoder5 = new PngBitmapEncoder();
                //    encoder5.Frames.Add(BitmapFrame.Create(decodedmap));
                //    encoder5.Save(stream5);
                //    stream5.Close();
                //}



            }
            catch(Exception ex)
            {
                LoadingMap.IsActive = false;
                 Task.Run(() => { return UsersController.GetInstance().SendErrorMail("Failed to load the weather map.", ex.InnerException.Message); });
            }
            await BuildTimelineMarkers();
          //  List<int[]> hohoh =   await Task.Run(() => { return WeatherController.GetInstance().FetchWheaterAnim(); });


          //Storyboard storyboard = new Storyboard();
          //ObjectAnimationUsingKeyFrames kf = new ObjectAnimationUsingKeyFrames();
          //Storyboard.SetTargetProperty(kf, new PropertyPath(Image.SourceProperty));
          //kf.Duration = TimeSpan.FromSeconds(17);
          //kf.RepeatBehavior = RepeatBehavior.Forever;
          //kf.FillBehavior = FillBehavior.Stop;
          //for (int i = 0; i < 100; i++)
          //{
          //    int[] dec = null;
          //    dec = hohoh[i];
          //    if (hohoh[i] == null)
          //    {
          //        dec = hohoh[i - 1];
          //        if (hohoh[i-1] == null)
          //        {
          //            dec = hohoh[i -2];

          //        } 
          //    } 
              
          //    WriteableBitmap decodedmap = new WriteableBitmap(500, 500, 96, 96, PixelFormats.Pbgra32, null);
              
          //    decodedmap.WritePixels(new Int32Rect(0, 0, decodedmap.PixelWidth, decodedmap.PixelHeight), dec, 4 * decodedmap.PixelWidth, 0);
          //    kf.KeyFrames.Add(new DiscreteObjectKeyFrame(decodedmap, KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(166 * i))));
          //}

          //storyboard.Children.Add(kf);

          //// Start the storyboard
          //storyboard.Begin(MapOverlayImage);


           // Rule r = new Rule();
           // r.Condition = new ComplexCondition() { LeftSideCondition = new ComplexCondition() { LeftSideCondition = new SimpleCondition(SensorsController.GetInstance().Sensors.ElementAt(0), SimpleCondition.BiggerThanOperator, 10), Operator = ComplexCondition.AndOperator, RightSideCondition = new SimpleCondition(SensorsController.GetInstance().Sensors.ElementAt(1), SimpleCondition.BiggerThanOperator, 10) }, Operator = ComplexCondition.AndOperator, RightSideCondition = new SimpleCondition(SensorsController.GetInstance().Sensors.ElementAt(2), SimpleCondition.BiggerThanOperator, 10) };
            //string sqlres = r.GetSQLCondition();
        
        
        }
    }
}
